//
//  ShareClientOAuthInfo.m
//  ShareCenter
//
//  Created by yutao on 14-2-18.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "ShareClientOAuthInfo.h"
#import "SFHFKeychainUtils.h"
#import "NSDate+Helper.h"

@implementation ShareClientOAuthInfo

+(id)defaultShareClientOAuth
{
    return [[self alloc] init];
}


- (ShareClientOAuthInfo *)oAuthInfoWithServiceType:(NSString *)type
{
    return [self readAuthorizeDataFromKeychainWithServiceType:type];
}
- (NSString *)oAuthValueForKey:(NSString *)key shareServiceType:(NSString *)serviceType
{
    return [SFHFKeychainUtils getPasswordForUsername:key andServiceName:serviceType error:nil];
}
- (BOOL)saveOAuthInfo:(ShareClientOAuthInfo *)oAuth serviceType:(NSString *)type
{
    return [self saveAuthorizeDataToKeychain:oAuth serviceType:type];
}
- (BOOL)deleteOAuthInfo:(ShareClientOAuthInfo *)oAuth serviceType:(NSString *)type
{
    return [self deleteAuthorizeDataInKeychain:oAuth serviceType:type];
}

- (BOOL)hasOauthInfoWithServiceType:(NSString *)type
{

    
	return [self oAuthInfoWithServiceType:type].userIDKey && [self oAuthInfoWithServiceType:type].accessTokenKey && [self oAuthInfoWithServiceType:type].expirationDateKey;
}

- (BOOL)isAuthorizeExpiredWithServiceType:(NSString *)type
{
	return [[NSDate date] compare:[NSDate dateFromString:[self oAuthInfoWithServiceType:type].expirationDateKey]] == NSOrderedDescending;
}

- (BOOL)isAuthValidWithServiceType:(NSString *)type
{
	if ([self hasOauthInfoWithServiceType:type]
        && ![self isAuthorizeExpiredWithServiceType:type]) {
		return YES;
	}else{
        return NO;
    }
    
}

- (BOOL)saveAuthorizeDataToKeychain:(ShareClientOAuthInfo *)oAuth serviceType:(NSString *)type
{
	BOOL	tokenFlag	= [SFHFKeychainUtils storeUsername:kShareClientOAuthInfo_accessToken andPassword:oAuth.accessTokenKey forServiceName:type updateExisting:YES error:nil];
	BOOL	openidFlag	= [SFHFKeychainUtils storeUsername:kShareClientOAuthInfo_userId andPassword:oAuth.userIDKey forServiceName:type updateExisting:YES error:nil];
	BOOL	expireFlag	= [SFHFKeychainUtils storeUsername:kShareClientOAuthInfo_expire andPassword:oAuth.expirationDateKey forServiceName:type updateExisting:YES error:nil];
	return tokenFlag && openidFlag && expireFlag ;
}

- (ShareClientOAuthInfo *)readAuthorizeDataFromKeychainWithServiceType:(NSString *)type
{
    
	self.accessTokenKey		= [SFHFKeychainUtils getPasswordForUsername:kShareClientOAuthInfo_accessToken andServiceName:type error:nil];
	self.userIDKey			= [SFHFKeychainUtils getPasswordForUsername:kShareClientOAuthInfo_userId andServiceName:type error:nil];
	self.expirationDateKey	= [SFHFKeychainUtils getPasswordForUsername:kShareClientOAuthInfo_expire andServiceName:type error:nil];
    
	return self;
}

- (BOOL)deleteAuthorizeDataInKeychain:(ShareClientOAuthInfo *)oAuth serviceType:(NSString *)type
{
	BOOL	tokenFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientOAuthInfo_accessToken andServiceName:type error:nil];
	BOOL	openidFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientOAuthInfo_userId andServiceName:type error:nil];
	BOOL	expireFlag		= [SFHFKeychainUtils deleteItemForUsername:kShareClientOAuthInfo_expire andServiceName:type error:nil];
    
	return tokenFlag && openidFlag && expireFlag;
}

@end

@implementation ShareClientOAuthInfoSina
static ShareClientOAuthInfoSina *shareClientSina = nil;

+(id)defaultShareClientOAuth
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareClientSina = [[self alloc] init];
    });
    return shareClientSina;
}
@end

@implementation ShareClientOAuthInfoQQ

static ShareClientOAuthInfoQQ *shareClientQQ = nil;

+(id)defaultShareClientOAuth
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareClientQQ = [[self alloc] init];
    });
    return shareClientQQ;
}

@end
